package com.gy.event;

import com.gy.componit.Battery;
import com.gy.componit.SimpleBullet;
import com.gy.game.Game;
import lombok.Data;

import java.math.BigDecimal;

/**
 * 开火事件
 * @author Dsy
 * @create 2022-02-04  1:38
 */
@Data
public class FireEvent implements GameEvent{

    private Battery battery;

    @Override
    public void invoke(Game game) {
        double direction = battery.getCurrDirection();
        double radians = Math.toRadians(direction);
        BigDecimal vy = new BigDecimal(Math.sin(radians));
        BigDecimal vx = new BigDecimal(Math.cos(radians));
        SimpleBullet bullet = new SimpleBullet();
        BigDecimal size = new BigDecimal(battery.getSize() * 2);
        bullet.setX(battery.getX().add(vx.multiply(size)));
        bullet.setY(battery.getY().subtract(vy.multiply(size)));
        bullet.setTeam(battery.getTeam());
        bullet.setColor(battery.getTeam().getTeamColor().darker().darker());
        bullet.setVx(vx);
        bullet.setVy(vy.negate());
        game.registerComponent(bullet, false);
    }
}
